package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.bean.QuiryDetailBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.listener.MyTextWatcher;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.SpanUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.widget.StateButton;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.cashregister.R.id.backe_paid_cause;


/**
 * 退洗
 * Created by ukaodev on 2017/6/2.
 */

public class BackWashDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.close_btn)
    ImageView closeBtn;
    @BindView(R.id.select_clothing_hite)
    TextView selectClothingHite;
    @BindView(R.id.orderNo)
    TextView orderNo;
    @BindView(R.id.name_phone)
    TextView namePhone;
    @BindView(R.id.statusText)
    TextView statusText;
    @BindView(R.id.cancel)
    StateButton cancel;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.paidPrice)
    TextView paidPrice;
    @BindView(R.id.backe_paidPrice)
    TextView backePaidPrice;
    @BindView(R.id.can_back_pice)
    TextView canBackPice;
    @BindView(R.id.import_pice)
    EditText importPice;
    @BindView(R.id.import_pice_hite)
    TextView importPiceHite;
    @BindView(R.id.title)
    TextView title;
    @BindView(backe_paid_cause)
    TextView backePaidCause;
    @BindView(R.id.backe_cause)
    EditText backeCause;

    private QuiryDetailBean mQuiryDetailBean;
    private String strPaidPrice;
    private boolean isOrderRefund;
    private String refundablePrice;


    public static BackWashDialogFragment newInstance(QuiryDetailBean mQuiryDetailBean, boolean isOrderRefund) {
        BackWashDialogFragment fragment = new BackWashDialogFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, mQuiryDetailBean);
        args.putBoolean(ARG_PARAM4, isOrderRefund);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mQuiryDetailBean = (QuiryDetailBean) getArguments().getSerializable(ARG_PARAM1);
            isOrderRefund = getArguments().getBoolean(ARG_PARAM4, false);
        }

    }

    private OnDialogSureClickListener mOnDialogSureClickListener;

    public void setOnSureClickListener(OnDialogSureClickListener mOnDialogSureClickListener) {
        this.mOnDialogSureClickListener = mOnDialogSureClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_back_wash_layout, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
        fillData();
        initListener();
    }

    @Override
    protected void initListener() {
        super.initListener();
        importPice.addTextChangedListener(mTextWatcher);
    }

    TextWatcher mTextWatcher = new MyTextWatcher() {


        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            String count = s.toString().trim();
            Constant.calculate(count, importPice);
            if (!count.isEmpty() && mQuiryDetailBean.getPayStatus()) {
                try {
                    double douPice = Double.parseDouble(count);
                    double douPaidPric = Double.parseDouble(refundablePrice);
                    L.i("douPice=" + douPice + "douPaidPric=" + douPaidPric);
                    canBackPice.setVisibility(douPice > douPaidPric ? View.VISIBLE : View.GONE);
                    submit.setEnabled(douPice > douPaidPric ? false : true);
                } catch (Exception e) {
                    canBackPice.setVisibility(View.GONE);
                    submit.setEnabled(true);
                }


            }
        }
    };


    private void fillData() {
        backePaidCause.setText(mustSpanText("*","退款原因",R.color.black));
        title.setText("衣物退洗");
        orderNo.setText("订单号：" + mQuiryDetailBean.getOrderNo());
        namePhone.setText("客户：" + mQuiryDetailBean.getUserInfo().getName() + "  " + mQuiryDetailBean.getUserInfo().getPhone());
        List<QuiryDetailBean.ProductListBean> mproductList = mQuiryDetailBean.getProductList();
        calculateClotingCount(mproductList);
        if (mQuiryDetailBean.getPayStatus()) {
                /*payStatus0	未支付1	已支付*/

            strPaidPrice = CheckUtils.isEmptyNumber(mQuiryDetailBean.getPaidPrice());
            refundablePrice = CheckUtils.isEmptyNumber(mQuiryDetailBean.getRefundablePrice());/*可退款金额*/
            paidPrice.setText("实付金额：" + strPaidPrice);
            statusText.setText(mustSpanText("支付状态：", "已付款", R.color.green));
            backePaidPrice.setText(mustSpanText("可退金额：", "￥" + refundablePrice, R.color.green));
            if (mQuiryDetailBean.getRefundablePrice() == 0) {
                importPice.setText("0");
                importPice.setTextColor(getResource().getColor(R.color.light_gray));
                importPice.setEnabled(false);
            }
        }else{
            importPiceHite.setText("修改订单总额为：");
            strPaidPrice = CheckUtils.isEmptyNumber(mQuiryDetailBean.getPayablePrice());
            backePaidPrice.setVisibility(View.GONE);
            paidPrice.setText("订单总额：" + strPaidPrice);
            statusText.setText(mustSpanText("支付状态：", "未付款", R.color.card_red));
        }






    }


    private void calculateClotingCount(List<QuiryDetailBean.ProductListBean> mproductList) {
        int clothingCount = 0;
        int selectClothingCount = 0;
        for (QuiryDetailBean.ProductListBean productListBean : mproductList) {

            if (productListBean.getType() == 1) {
                clothingCount = clothingCount + 1;
            }

            if (productListBean.isCheck() && productListBean.getType() == 1) {/*衣物*/
                selectClothingCount = selectClothingCount + 1;
            }

        }
        String strSelecthite = "";
        if (selectClothingCount != 0 && !isOrderRefund) {
            strSelecthite = ",已选中" + selectClothingCount + "件衣物";
        }
        selectClothingHite.setText("该订单共" +  clothingCount+ "件衣物" + strSelecthite);
    }

    protected CharSequence mustSpanText(String hite, String colorHite, @ColorRes int id) {
        CharSequence mCharSequence = new SpanUtils().append(hite).append(colorHite).setForegroundColor(getColors(id)).create();
        return mCharSequence;
    }





    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.close_btn, R.id.cancel, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.cancel:
                dismiss();
                break;
            case R.id.submit:


                String importpice = getText(importPice);
                String strbackeCause = getText(backeCause);


                if (importpice.isEmpty()){
                    T.show("请输入退款金额");
                    return;
                }else  if (strbackeCause.isEmpty() ){
                    T.show("请输入退款原因");
                    return;
                }


                if (mOnDialogSureClickListener!=null) {
                    HashMap   mHashMap=new HashMap();
                    mHashMap.put(ARG_PARAM1,importpice);
                    mHashMap.put(ARG_PARAM2,strbackeCause);
                    mHashMap.put(ARG_PARAM3,"");
                    mOnDialogSureClickListener.onSureItemClick(mHashMap);

                }
                dismiss();
                break;
        }
    }
}
